package hernanbosqued.parking;

import android.support.v7.widget.Toolbar;
import android.support.v7.widget.Toolbar.OnMenuItemClickListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.res.Configuration;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import proguide.library.Utils;

//Atlanta desde casa 3

public class ActivityMain extends Activity implements OnMenuItemClickListener ,LocationListener
{
	private final int   LOCATION_UPDATE_METERS = 100;
	private HandlerMain	handler_main;
	private WebView	    webview;
	AdapterParkings	    adapter;
	LocationManager	    location_manager;
	Dialog			    progress_dialog;
	
	@SuppressLint( { "JavascriptInterface", "SetJavaScriptEnabled" } )
	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_main );

		double level = Utils.get_battery_level(this );

		handler_main = new HandlerMain( this );
		
		Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
		toolbar.setTitle( getString( R.string.app_name ) );
		toolbar.inflateMenu( R.menu.activity_main );
		toolbar.setOnMenuItemClickListener( this );

		progress_dialog = Util.getProgressDialog( this );
		progress_dialog.setOnCancelListener( new OnCancelListener( )
		{
			@Override
			public void onCancel( DialogInterface dialog )
			{
				finish( );
			}
		} );

		webview = new WebView( this );
		webview.getSettings( ).setJavaScriptEnabled( true );
		webview.addJavascriptInterface( new JavascriptBridge( handler_main ), "bridge" );
		webview.setWebViewClient( new WebViewClient( )
		{
			@Override
			public void onPageFinished( WebView view, String url )
			{
				super.onPageFinished( view, url );
				webview.loadUrl( "javascript:bridge.getParkings( JSON.stringify( parkings ) );" );
			}
		} );
		
		progress_dialog.show( );
		webview.loadUrl( getString( R.string.url ) );
	}

	@Override
	protected void onStart()
	{
		super.onStart( );
		location_manager = (LocationManager) ( getSystemService( Context.LOCATION_SERVICE ) );
		location_manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, LOCATION_UPDATE_METERS, this );
	}

	@Override
	protected void onStop()
	{
		location_manager.removeUpdates( this );
		super.onStop( );
	}

	@Override
	public void onConfigurationChanged( Configuration newConfig )
	{
		super.onConfigurationChanged( newConfig );
	}
	
	@Override
	public boolean onMenuItemClick( MenuItem item )
	{
		if ( item.getItemId( ) == R.id.action_sync )
		{
			progress_dialog.show( );
			webview.loadUrl( getString( R.string.url ) );
			return true;
		}
		return false;
	}

	@Override
	public void onLocationChanged( Location location )
	{
		if ( adapter != null && adapter.getCount( ) > 1 )
		{
			adapter.sortByDistance( location );
		}
	}

	@Override
	public void onStatusChanged( String provider, int status, Bundle extras )
	{
	}

	@Override
	public void onProviderEnabled( String provider )
	{
	}

	@Override
	public void onProviderDisabled( String provider )
	{
		Toast.makeText( this, getString( R.string.warning_no_gps ), Toast.LENGTH_SHORT ).show( );
	}
}